'use strict';
let url = "https://news.topurl.cn/api";

const Service = require('egg').Service;

class NewsService extends Service {
  async getData() {
    let data = await this.ctx.curl(url);
    // console.log(data.data.toString());
    data = JSON.parse(data.data.toString());
    return data;
  }

  async dayNum(year, month, day) {
    let count = 0;
    switch (month) {
      case 1:
        count = day;
        break;
      case 2:
        count = 31 + day;
        break;
      case 3:
        count = 59 + day;
        break;
      case 4:
        count = 90 + day;
        break;
      case 5:
        count = 120 + day;
        break;
      case 6:
        count = 151 + day;
        break;
      case 7:
        count = 181 + day;
        break;
      case 8:
        count = 212 + day;
        break;
      case 9:
        count = 243 + day;
        break;
      case 10:
        count = 273 + day;
        break;
      case 11:
        count = 304 + day;
        break;
      case 12:
        count = 334 + day;
        break;
      default:
        break;
    }
    if (year % 4 == 0 && month > 2) {
      //闰年
      count++;
    }
    return count;
  }

  async jindu(num) {
    let Img = ``;
    switch (num) {
      case 0:
        Img = `▓░░░░░░░░░░`;
        break;
      case 1:
        Img = `▓▓░░░░░░░░░`;
        break;
      case 2:
        Img = `▓▓▓░░░░░░░░`;
        break;
      case 3:
        Img = `▓▓▓▓░░░░░░░`;
        break;
      case 4:
        Img = `▓▓▓▓▓░░░░░░`;
        break;
      case 5:
        Img = `▓▓▓▓▓▓░░░░░`;
        break;
      case 6:
        Img = `▓▓▓▓▓▓▓░░░░`;
        break;
      case 7:
        Img = `▓▓▓▓▓▓▓▓░░░`;
        break;
      case 8:
        Img = `▓▓▓▓▓▓▓▓▓░░`;
        break;
      case 9:
        Img = `▓▓▓▓▓▓▓▓▓▓░`;
        break;

      default:
        break;
    }
    return Img;
  }

  async getbarText() {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    // console.log(year,month,day);
    let num = await this.dayNum(year, month, day);
    // console.log(num);
    let countP = 0;
    if (year % 4 == 0) {
      countP = (num / 366) * 100;
    } else {
      countP = (num / 365) * 100;
    }
    let progressBar = await this.jindu(parseInt(countP / 10));
    let barText = `${progressBar}\n${year}年，您已经使用了${countP.toFixed(2)}%`
    return barText;
  }

  async processNews() {
    let data = await this.getData();
    // 日期
    let calendar = data.data.calendar;
    // 新闻列表
    let newsList = data.data.newsList;
    let newsText = '';
    newsList.forEach((item, index) => {
      newsText = newsText + `${index+1}、${item.title}\n`
    });
    // 历史上的今天
    let historyList = data.data.historyList;
    let historyText = '';
    historyList.forEach((item, index) => {
      historyText = historyText + `${item.event}\n`
    });
    // 每日寄语
    let sentence = `${data.data.sentence.sentence}` + `——${data.data.sentence.author}`
    let barText = await this.getbarText();
    let text = `今天是${calendar.cYear}年，${calendar.cMonth}月${calendar.cDay}日。农历${calendar.lMonth}月${calendar.lDay}日\n\n` + `${newsText}` + `\n【历史上的今天】\n` + `${historyText}` + `\n【每日寄语】\n` + `${sentence}\n` + `\n【进度条】\n${barText}`;

    return text;
  }

  async sendNews(res) {
    let sendMsg = this.ctx.service.sendMsg;
    let text = await this.processNews();
    sendMsg.sendToGroup({
      id: res.group_id,
      msg: text
    });
  }
}

module.exports = NewsService;